package me.szp;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class Main {


    private static int x;

    private static int y;

    public static void main(String[] args) {

        int[][] define = new int[][]{{2, 3}, {4, 5}, {6, 7}};

        arrange(define, 30);
    }


    public static List<Seat> arrange(int[][] define, int count) {

        // 检查格式
        check(define);

        List<Seat> seatList = initList(define);

        // 筛选出过道的
        List<Seat> aisleSeatList = seatList
                .stream()
                .filter(e -> e.getLocation().equals(Location.AISLE))
                // 先从前到后排序
                .sorted(Comparator.comparing(Seat::getRow))
                // 再从左到右排序
                .sorted(Comparator.comparing(Seat::getCol))
                .collect(Collectors.toList());

        // 筛选出靠窗的
        List<Seat> windowSeatList = seatList
                .stream()
                .filter(e -> e.getLocation().equals(Location.WINDOW))
                // 先从前到后排序
                .sorted(Comparator.comparing(Seat::getRow))
                // 再从左到右排序
                .sorted(Comparator.comparing(Seat::getCol))
                .collect(Collectors.toList());


        return seatList;

    }


    private static List<Seat> initList(int[][] define) {

        List<Seat> list = new ArrayList<>();

        for (int k = 0; k < define.length; k++) {

            int row = define[k][1];
            int col = define[k][0];

            List<Seat> seatList = convert(row, col);

            // 1. 最左边
            if (k == 0) {
                for (Seat seat : seatList) {
                    // 第一列,为靠窗
                    if (seat.getCol() - y == 0) {
                        seat.setLocation(Location.WINDOW);
                    }
                    // 最后一列，为过道
                    if (seat.getCol() - y == col - 1) {
                        seat.setLocation(Location.AISLE);
                    }
                }
            }
            // 2. 最右边
            else if (k == define.length - 1) {
                for (Seat seat : seatList) {
                    // 第一列,为过道
                    if (seat.getCol() - y == 0) {
                        seat.setLocation(Location.AISLE);
                    }
                    // 最后一列，为靠窗
                    if (seat.getCol() - y == col - 1) {
                        seat.setLocation(Location.WINDOW);
                    }
                }
            }
            // 中间部分
            else {
                for (Seat seat : seatList) {
                    // 第一列,为过道
                    if (seat.getCol() - y == 0) {
                        seat.setLocation(Location.AISLE);
                    }
                    // 最后一列，为过道
                    if (seat.getCol() - y == col - 1) {
                        seat.setLocation(Location.AISLE);
                    }
                }
            }

            list.addAll(seatList);
        }

        return list;
    }

    private static List<Seat> convert(int row, int col) {
        List<Seat> seatList = new ArrayList<>();
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                seatList.add(new Seat(x, y++, 0, Location.CENTER));
            }
            x++;
        }
        return seatList;
    }

    private static void check(int[][] define) {
        if (define == null || define.length == 0) {
            throw new IllegalArgumentException("模型定义不能为空");
        }

        for (int[] ints : define) {
            if (ints == null || ints.length != 2) {
                throw new IllegalArgumentException("模型定义不合法");
            }
        }
    }


}
